import { size, IFormProps, pageSize, dateTimeFormat } from "../../utils/common";
import React from "react";
import { connect } from "dva";
import { Form, Row, Col, Button, Input, Table, Popconfirm, Select } from 'antd';
import { PaginationProps } from "antd/lib/pagination";
import moment from 'moment';
const Styles = require("../../styles/index.less");
import * as api from '../../api/api';
import StoreVerifyEdit from "../../components/stores/StoreVerifyEdit";

export interface IStoresPros extends IFormProps {
    store: any
}
class VerifyStores extends React.PureComponent<IStoresPros, any>{
    constructor(props) {
        super(props);
        this.state = {}
    }

    search = () => {
        this.getStores();
    }

    getStores = (payload?) => {
        payload = Object.assign({}, this.props.store.storeVerifySearchs, this.props.form.getFieldsValue(), { ...payload });
        this.props.dispatch({
            type: "store/getVerifyStores",
            payload
        });
    }

    reset = () => {
        this.props.form.resetFields();
    }

    edit = (store = {}) => {
        this.props.dispatch({
            type: "store/save",
            payload: { store }
        });
        this.setVisible(true);
    }

    setVisible = (visible = false) => {
        this.setState({ visible });
    }

    save = (form) => {
        let { store } = this.props.store;
        form = Object.assign({}, store, form);
        this.props.dispatch({
            type: "store/verifyStore",
            payload: {
                form,
                callback: () => {
                    this.setVisible();
                }
            }
        });
    }

    render(): JSX.Element {
        //console.log(this.props);
        const { getFieldDecorator } = this.props.form;
        const { loading } = this.props;
        const { store, stores: { totalCount, items }, storeVerifySearchs: { pageIndex, pageSize, keywords } } = this.props.store;
        const {businessScope,businessScopes:{items:bitems}}=this.props.store;
        const pageChange = (pageIndex, pageSize) => {
            let payload = Object.assign({},
                this.props.store.storeVerifySearchs,
                { pageIndex, pageSize });
            this.getStores(payload);
        }

        const pagination: PaginationProps | boolean = {
            total: totalCount,
            defaultCurrent: 1,
            current: pageIndex,
            pageSize: pageSize,
            showSizeChanger: true,
            showTotal: (total) => `共 ${total} 条`,
            showQuickJumper: true,
            pageSizeOptions: ["10", "20", "30", "50"],
            onShowSizeChange: pageChange,
            onChange: pageChange
        };

        return (
            <Row className={Styles.F_BG_WHITE1}>
                <Row className={Styles.F_BG_WHITE}>
                    <Col span={24}>
                        <Form layout="inline">
                            <Form.Item label='审核状态'>
                                {getFieldDecorator('status', {
                                    initialValue: '0'
                                })(<Select style={{ width: 120 }}>
                                    <Select.Option value=''>全部</Select.Option>
                                    <Select.Option value='0'>待审核</Select.Option>
                                    <Select.Option value='1'>已通过</Select.Option>
                                    <Select.Option value='-1'>已驳回</Select.Option>
                                </Select>)}
                            </Form.Item>
                            <Form.Item>
                                {getFieldDecorator("keywords", {
                                    initialValue: keywords
                                })(<Input
                                    style={{ width: 200 }}
                                    size={size}
                                    placeholder="输入商户名/商户描述"
                                />)}
                            </Form.Item>
                            <Form.Item>
                                <Button size={size} type="primary" loading={loading} onClick={this.search}>查询</Button>
                            </Form.Item>
                        </Form>
                        <Col span={24}>
                            <hr className={Styles.hrBorder} />
                            <h3>店铺申请列表</h3>
                        </Col>
                        <Col span={24}>
                            <Table
                                dataSource={items}
                                bordered
                                pagination={pagination}
                                rowKey={(record: api.SingleUserMallStoresVOStoreVO) => String(record.id)}
                                loading={loading}
                            >
                                <Table.Column title="序号" key="no" render={(text, record, index) => {
                                    return index + 1
                                }} />
                                <Table.Column title="店铺名称" key="name" dataIndex="name" />
                                <Table.Column title="经营范围" key="businessScope" dataIndex="businessScope" />
                                <Table.Column title="申请时间" key="creationTime" render={(record: any) => {
                                    return record.creationTime && moment(record.creationTime).format(dateTimeFormat);
                                }} />
                                <Table.Column title="申请类型" key="authType" dataIndex="authType" />
                                <Table.Column title="申请图片" key="certImages" render={(text, record: api.SingleUserMallStoresVOStoreVO, index) => {
                                    return record.certImages && record.certImages.split(',').map((image) => {
                                        return <img width='100px' src={image} />
                                    })
                                }} />
                                <Table.Column title="状态" key="statusView" dataIndex="statusView" />
                                <Table.Column title="审核备注" key="remark" dataIndex="remark" />
                                <Table.Column title="操作" render={(text, record: api.SingleUserMallStoresVOStoreVO, index) => {
                                    if(record.statusView == "待审核")
                                    {
                                        return (
                                            <div>
                                                <a className={Styles.M_RIGHT} onClick={() => this.edit(record)}>
                                                    审核
                                                </a>
                                            </div>
                                        );
                                    }
                                }} />
                            </Table>
                        </Col>
                    </Col>
                </Row>
                {
                    this.state.visible ? (<StoreVerifyEdit visible={this.state.visible}
                        store={store}
                        onSave={this.save}
                        businessScope={businessScope}
                        businessScopes={bitems}
                        onCancel={this.setVisible} />) :
                        (undefined)
                }
            </Row>)
    }
}

export default connect(({ store, loading: { models: { store: loading } } }) => ({ store, loading }))(Form.create()(VerifyStores));
